﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;  

namespace Game1_Resist50
{
    public partial class FormGame : Form
    {
        [DllImport("user32.dll", EntryPoint = "ShowCursor", CharSet = CharSet.Auto)]
        public extern static void ShowCursor(int status);   

        public FormGame()
        {
            InitializeComponent();

            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
        }
        
        private void FormGame_Load(object sender, EventArgs e)
        {
            GraphicsHelper.BmpWidth = this.ClientRectangle.Width;
            GraphicsHelper.BmpHeight = this.ClientRectangle.Height;
            Debug.Print(
                string.Format("Client w = {0}; h = {1}", 
                GraphicsHelper.BmpWidth, 
                GraphicsHelper.BmpHeight));
            _gHelper = GraphicsHelper.Instance();

            // Player initialization.
            Player.Instance().SetPosition(GraphicsHelper.BmpWidth / 2, GraphicsHelper.BmpHeight / 2);
            
            _manager = GameObjectsManager.Instance();
            _manager.AddGameObject(Player.Instance());
        }

        private GraphicsHelper _gHelper = null;
        private GameObjectsManager _manager = null;
        private int _maxTimeCount = 10;
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (_isGameStart)
            {
                if (_manager.CollisionDetected())
                {
                    // Stop the game.
                    _isGameStart = false;
                    timerMonsterBorn.Enabled = false;
                    timerGameTick.Enabled = false;

                    if (_timeCount >= _maxTimeCount)
                    {
                        textBoxWin.Visible = true;
                    }
                    else
                    {
                        textBoxLose.Visible = true;
                    }
                    buttonRetry.Visible = true;

                    _gHelper.ClearBackground(Color.AliceBlue);
                    

                    // Win32 API.
                    ShowCursor(1);
                }
                else
                {
                    _gHelper.ClearBackground();
                    _gHelper.DrawAllGameObjects();
                    e.Graphics.DrawImage(_gHelper.WholeImage, 0, 0);
                    textBoxTimeCount.Text = string.Format("Time: {0}s", _timeCount);
                }
            }
        }

        private void timerGameTick_Tick(object sender, EventArgs e)
        {

            _manager.UpdateObjectsPosition();
            Refresh();
        }

        private void FormGame_MouseMove(object sender, MouseEventArgs e)
        {
            var player = Player.Instance();
            player.SetPosition(e.X, e.Y);
        }

        static int _monsterCount = 1;
        static int _timeCount = 0;
        private void timerMonsterBorn_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < _monsterCount; i++)
            {
                _manager.AddGameObject(new Monster());
            }
            _monsterCount += 1;
            _timeCount++;
        }

        private bool _isGameStart = false;
        private void buttonStart_Click(object sender, EventArgs e)
        {
            buttonStart.Visible = false;
            textBoxTitle.Visible = false;

            // Win32 API.
            ShowCursor(0);

            timerGameTick.Enabled = true;
            timerMonsterBorn.Enabled = true;
            _isGameStart = true;
        }

        private void buttonRetry_Click(object sender, EventArgs e)
        {
            // Reset the game.
            _timeCount = 0;
            _monsterCount = 1;

            buttonRetry.Visible = false;
            textBoxLose.Visible = false;
            textBoxWin.Visible = false;

            // Win32 API.
            ShowCursor(0);

            _isGameStart = true;
            _manager.Initialize();
            timerGameTick.Enabled = true;
            timerMonsterBorn.Enabled = true;
        }
    }
}
